package com.xbongbong.paas.service.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.pojo.dto.CustomPkSetDTO;
import com.xbongbong.paas.pojo.vo.CustomPkSetVO;
import com.xbongbong.paas.service.CompanyConfigService;

import com.xbongbong.paas.service.CustomPkService;
import com.xbongbong.paas.service.PackageService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.constant.PackageConstant;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.enums.PackageStatusEnum;
import com.xbongbong.pro.enums.PackageTypeEnum;
import com.xbongbong.pro.enums.ValueAddedTypeEnum;
import com.xbongbong.pro.service.toolbox.statistic.help.ChartPkRuleHelp;
import com.xbongbong.pro.statistic.model.ChartCategoryModel;
import com.xbongbong.pro.statistic.model.ChartCustomModel;
import com.xbongbong.pro.statistic.model.ChartModel;
import com.xbongbong.saas.help.AppModuleHelp;
import com.xbongbong.saas.help.MarketManagementBusinessHelp;
import com.xbongbong.saas.service.toolbox.help.PackageStatusHelp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Objects;

/**
 * @author weiming
 * @version v1.0
 * @ProjectName xbb-pro
 * @Description: TODO
 * @date 2021/4/12 13:47
 * @since v1.0
 */
@Service("customPkSetService")
public class CustomPkServiceImpl implements CustomPkService {

    private static final Logger LOG = LoggerFactory.getLogger(CustomPkServiceImpl.class);

    @Resource
    private PackageStatusHelp packageStatusHelp;
    @Resource
    private PackageService packageService;
    @Resource
    private ChartCustomModel chartCustomModel;


    @Override
    public CustomPkSetVO saveCustomPk(CustomPkSetDTO customPkSetDTO) throws XbbException {
        String corpid = customPkSetDTO.getCorpid();
        CustomPkSetVO customPkSetVO = new CustomPkSetVO();
        //判断是否是旗舰版、高级版 并且有自定义图表数量，反之则提示升级到更高版本
        Integer feeType = packageService.getFeeType(corpid);
        //如果既不是高级版也不是旗舰版
        if(!Objects.equals(PackageTypeEnum.ADVANCED.getType(), feeType) && !Objects.equals(PackageTypeEnum.ULTIMATE.getType(), feeType)){
            customPkSetVO.setMessage(I18nMessageUtil.getMessage(PackageConstant.VERSION_MESSAGE));
            return customPkSetVO;
        }
        //判断数量是否查过限制。
        Integer status = packageStatusHelp.getPackageStatus(corpid, ValueAddedTypeEnum.CUSTOM_CHART_NUM, 1);
        if (!Objects.equals(status, PackageStatusEnum.NORMAL.getCode()) && !Objects.equals(status, PackageStatusEnum.WARNING.getCode())) {
            customPkSetVO.setMessage(String.format(I18nMessageUtil.getMessage(PackageConstant.CUSTOM_PK_SIZE_MESSAGE), 1));
            return customPkSetVO;
        }
        //以上限制都排除，接着进入保存阶段。自定义的pk榜会保存在tb_chart_custom
        ChartEntity chartEntity = new ChartEntity();
        BeanUtils.copyProperties(customPkSetDTO , chartEntity);
        chartEntity.setStatisticsType(2);
        chartCustomModel.insert(chartEntity);
        return customPkSetVO;
    }
}
